package com.web.startup;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;


/**
 * Created with IntelliJ IDEA.
 * User: xf
 * Date: 2016/4/28
 * Time: 20:24
 * To change this template use Editor | File and Code Templates.
 */
@Configuration
@EnableRedisRepositories(basePackages = "com.redis")
@EnableCaching
public class RedisConfig {

    /**
     * 开启第三方redis缓存管理器
     *
     * @return
     */
    /*@Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        return new RedisCacheManager(redisTemplate);
    }*/

    /**
     * redis连接工厂
     *
     * @return
     */
    @Bean
    public JedisConnectionFactory redisCF() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName("localhost");
        jedisConnectionFactory.setPort(6379);
        jedisConnectionFactory.setPassword("123456");
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    /**
     * redis模版
     *
     * @param cf
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory cf) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(cf);
        template.afterPropertiesSet();
        return template;
    }

}
